
alter   procedure p_voucherinit
  @vouyearmonth varchar(6),
  @bdate datetime,
  @vouyearmonth1 varchar(6),
  @bdate1 datetime,
  @filid int 
as  
 
begin
  set nocount on
  set xact_abort on 
  if (exists(select * from tfb_voucher where voucherid=@filid*(-2)-1)) 
  begin
    if ((@vouyearmonth is not null) and (@bdate is not null) ) 
    update tfb_voucher
      set vouyearmonth=@vouyearmonth,
          bdate=@bdate,
          update_time= @bdate   
      where voucherid=@filid*(-2)-1;
  end
  else
  begin
    if (@vouyearmonth is null)  
      set @vouyearmonth='194910';

    if (@bdate is null)  
      set @bdate='10/01/1949';

    insert into tfb_voucher (bstate,bcode,
      voucherid         ,         
      create_by       ,         
      vouwordid     ,         
      orderno        ,         
      voucherno         ,         
      bdate       ,         
      update_time   ,         
      affix   ,         
      formertype     ,         
      formerbid   ,         
      vouyearmonth  ,         
      signer_by       ,         
      check_by      ,         
      binder_by       ,
      filid )
    values(1,@filid*(-2)-1,
      @filid*(-2)-1,
      0,
      1,
      0,
      0,
      @bdate ,
      @bdate ,
      1,
      1,
      null,
      @vouyearmonth,
      null,
      0,
      0,
      @filid);
  end

  if (exists(select * from tfb_voucher where voucherid=@filid*(-2)-2)) 
  begin
    if ( (@vouyearmonth1 is not null ) and (@bdate1 is not null) ) 
    update tfb_voucher
      set vouyearmonth=@vouyearmonth1,
          bdate=@bdate1,
          update_time= @bdate1   
      where voucherid=-2;
  end
  else
  begin
    if (@vouyearmonth1 is null)  
     set @vouyearmonth1='194910';

    if (@bdate1 is null)  
      set @bdate1='10/01/1949';

    insert into tfb_voucher (bstate,bcode,
      voucherid         ,         
      create_by       ,         
      vouwordid     ,         
      orderno        ,         
      voucherno         ,         
      bdate       ,         
      update_time   ,         
      affix   ,         
      formertype     ,         
      formerbid   ,         
      vouyearmonth  ,         
      signer_by       ,         
      check_by      ,         
      binder_by       ,
      filid)
    values(1,@filid*(-2)-2,
      @filid*(-2)-2,
      0,
      1,
      0,
      0,
      @bdate1 ,
      @bdate1 ,
      1,
      1,
      null,
      @vouyearmonth1,
      null,
      0,
      0,
      @filid);
  end
end




GO
